let page = {
	currentPage: 1,
	pageSize: 9,
	count: 0,
	goodList: [],
	total: 0,
	search: '',
	cat_id: '',
	search_type: 1,
	pageList: [9, 18, 27]
}

function render(currentPage, pageSize, searchType, value,resolve) {
	let params = 'page=' + currentPage + '&pagesize=' + pageSize;
	let address = '';
	if (searchType == 2) {
		params += '&catId=' + value;
		address = '/api_goods'
	} else {
		params += '&keywords=' + value;
		address = '/api_search';
	}
	ajax({
		method: "GET",
		type: "JSON",
		url: BASE_URL + address + "?" + params,
		callback: function(data) {
			page.goodList = data.data;
			page.count = page.count;
			page.total = data.page;
			if(page.total == undefined){
							page.total =  page.count % page.pageSize == 0 ? page.count / page.pageSize : Math.ceil(page.count / page.pageSize);
						}
						//处理细节
						if(page.currentPage > page.total){
							page.currentPage = page.total;
							//重新查询数据
							render(page.currentPage, pageSize, searchType, value, resolve)
						}

			let hotGoodList = document.querySelector(".hot>ul")
			let text = ''
			page.goodList.forEach(item => {
				text += "<li>" +
					"<div class='hot_img'>" +
					"<img src='" + item.goods_thumb + "'>" +
					"<p>良仓双12</p>" +
					"<img src='img/double12.jpg'>" +
					"</div>" +
					"<div class='good_info'>" +
					"<a href='goodDetails.html?goodsId="+item.goods_id+"'>" +
					"<span>" + item.goods_name + "</span><br>" +
					item.goods_desc +
					"</a>" +
					"</div>" +
					"<div class='good_other'>" +
					"<div>" +
					"<img src='" + item.brand_thumb + "'>" +
					"<span>" + item.brand_name + "</span>" +
					"<span class='nice'>" + item.star_number + "</span>" +
					"</div>" +
					"</div>" +
					"</li>"
			})
			hotGoodList.innerHTML = text;
			resolve();
		}
	})
}



function pageScope(currentPage, total) {
	let pageScope = [];
	//页面总数小于等于10
	let startPage = 1;
	let endPage = total;
	if (total > 10) {
		startPage = currentPage - 5;
		endPage = currentPage + 4;
		//首页溢出判断
		if (startPage <= 0) {
			startPage = 1;
			endPage = 10;
		}
		//尾页溢出
		if (endPage > total) {
			startPage = total - 9;
			endPage = total;
		}
	}

	for (let i = startPage; i <= endPage; i++) {
		pageScope.push(i);
	}
	return pageScope;
}


/* 
 * 渲染页面
 *current:当前页
 *count:商品总数 
 */
function renderPage(currentPage, pageSize, total) {
	//计算页面总数
	//获得页码数组
	let pages = pageScope(currentPage, total);
	//获取渲染列表对象
	let list = document.querySelector(".page_list>ul");
	list.innerHTML = '';
   console.log('开始渲染页码')
	if (pages.length > 0) {

		list.innerHTML += "<li><a href='#' class='cicle' onclick='previous()'><</a></li>";
		for (let value of pages) {
			if (value == currentPage) {
				list.innerHTML += "<li><a href='#' class='active' onclick='changePageNo(" + value + ")'>" + value +
					"</a></li>";
			} else {
				list.innerHTML += "<li><a href='#' class='cicle' onclick='changePageNo(" + value + ")'>" + value +
					"</a></li>";
			}
		}
		list.innerHTML += "<li><a href='#' class='cicle' onclick='next()'>></a></li>";
	}

	let pageInfo = document.getElementById("pageInfo");
	pageInfo.innerHTML = currentPage + "/" + total;

	if (pages.length == 0) {
		let pageChange = document.querySelector(".to_page");
		pageChange.style.display = "none";
	} else {
		let pageChange = document.querySelector(".to_page");
		pageChange.style.display = "inline-block";
	}

}



function changePageSize() {
	let pageSize = document.querySelector("select[name='pageSize']").value;
	page.pageSize = pageSize;
		if(page.search_type == 2){
			value = page.cat_id;
		}else{
			value = page.search;
		}
		new Promise((resolve, reject) => {
					//渲染商品数据
					render(page.currentPage, page.pageSize, page.search_type, value,resolve);
				}).then(()=>{
					//渲染页码数据
					renderPage(page.currentPage, page.pageSize, page.total);
				})
}

function changePageNo(pageNo) {
	page.currentPage = pageNo;
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	new Promise((resolve, reject) => {
				//渲染商品数据
				render(page.currentPage, page.pageSize, page.search_type, value,resolve);
			}).then(()=>{
				//渲染页码数据
				renderPage(page.currentPage, page.pageSize, page.total);
			})
}

function previous() {
	let pageNo = page.currentPage - 1;
	if (pageNo <= 0) {
		return;
	}
	page.currentPage = pageNo;
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	new Promise((resolve, reject) => {
				//渲染商品数据
				render(page.currentPage, page.pageSize, page.search_type, value,resolve);
			}).then(()=>{
				//渲染页码数据
				renderPage(page.currentPage, page.pageSize, page.total);
			})
}

function next() {
	let pageNo = page.currentPage + 1;
	if (pageNo > page.total) {
		return;
	}
	page.currentPage = pageNo;
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	new Promise((resolve, reject) => {
				//渲染商品数据
				render(page.currentPage, page.pageSize, page.search_type, value,resolve);
			}).then(()=>{
				//渲染页码数据
				renderPage(page.currentPage, page.pageSize, page.total);
			})
}

function toPageNo(){
	//修改页码添加监听监听
	let page_btn = document.querySelector("button[class='page_btn']");
	page_btn.addEventListener("click",()=>{
		let pageNo = document.querySelector("input[name='pageNo']").value;
		console.log(pageNo);
		if(pageNo == ''){
			alert('请输入页码');
			return;
		}
		if(pageNo <= 0 || pageNo > page.total){
			alert('页码错误');
			return;
		}
		page.currentPage = pageNo;
		let value = '';
		if(page.search_type == 2){
			value = page.cat_id;
		}else{
			value = page.search;
		}
	new Promise((resolve, reject) => {
				//渲染商品数据
				render(page.currentPage, page.pageSize, page.search_type, value,resolve);
			}).then(()=>{
				//渲染页码数据
				renderPage(page.currentPage, page.pageSize, page.total);
			})
	})
}


/* 页码加载初始化 */
window.onload = function() {
	let searchParams = new URLSearchParams(window.location.search);
	let search_type = searchParams.get('search_type')
	
	if (search_type != undefined || search_type != '') {
		page.search_type = search_type;
	}
	
	if (search_type == 2) {
		let cat_id = searchParams.get('cat_id');
		let cat_name = searchParams.get('cat_name');
	
		let type = document.querySelector(".type");
		type.innerHTML = '> ' + cat_name;
		page.cat_id = cat_id;
		new Promise((resolve, reject) => {
					//渲染商品数据
					render(page.currentPage, page.pageSize, page.search_type, cat_id,resolve);
				}).then(()=>{
					//渲染页码数据
					renderPage(page.currentPage, page.pageSize, page.total);
				})
		
	} else {
		let keywords = searchParams.get('keywords');
		console.log(keywords);
		new Promise((resolve, reject) => {
					//渲染商品数据
					render(page.currentPage, page.pageSize, page.search_type, keywords,resolve);
				}).then(()=>{
					//渲染页码数据
					renderPage(page.currentPage, page.pageSize, page.total);
				})
	}
    
	let select = document.querySelector("select[name='pageSize']");
	for (let value of page.pageList) {
		let option = document.createElement("option");
		option.innerHTML = value;
		select.appendChild(option);
	}
	
		(function(){
			ajax({
				method:'GET',
				type:'JSON',
				url:BASE_URL+'/api_cat',
				callback:function(data){
					//获取渲染的父模块
					let typeList = document.querySelector(".typeList>ul");
					let goodListData = data.data;
					goodListData.forEach((item, index) => {
						let li = document.createElement("li");
						li.innerHTML = "<a href='goodlist.html?search_type=2&cat_id="+item.cat_id+"&cat_name="+item.cat_name+"'>"+item.cat_name+"</a>";
						typeList.appendChild(li);
					});
				}
			})
		})();
}